package com.yan.config;


import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.yan.entity.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

@Component
public class DlxListener {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    public Set<Result> dlx = new HashSet<>();

    @RabbitListener(queues = RabbitConstants.DLX_QUEUE_NAME)
    public void getDirectMessage(Channel channel, Message message) throws IOException {
        String msg = new String(message.getBody());
        log.info("【mq接收到的消息】：{}", msg);
        try {
            Result result = JSON.parseObject(msg, Result.class);
            dlx.add(result);
            // 拒绝消息，requeue=false 表示不再重新入队，如果配置了死信队列则进入死信队列
//            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception e){
            // 拒绝消息，requeue=false 表示不再重新入队，如果配置了死信队列则进入死信队列
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            e.printStackTrace();
        }
    }
}
